package com.fieldchain.ex.fa100.ui.kline;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.event.BaseEvent;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/16
 */
public class KLineActivity extends MyBaseActivity implements View.OnClickListener {
    private TextView tvFs;
    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private RadioButton rb5;
    private TextView tvBack;

    private String market = "";
    private int timeTags = 1;
    private KlineFragment fragment;


    @Override
    public void findView() {
        tvFs = findViewById(R.id.tv_fs);
        rb0 = findViewById(R.id.rb0);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rb3 = findViewById(R.id.rb3);
        rb4 = findViewById(R.id.rb4);
        rb5 = findViewById(R.id.rb5);
        tvBack = findViewById(R.id.tv_back);
    }

    @Override
    public void setValue() {
        market = getIntent().getStringExtra("market");
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        tvBack.setOnClickListener(this);
        tvFs.setOnClickListener(this);


        setIndex(rb0, 60);
        setIndex(rb1, 15 * 60);
        setIndex(rb2, 30 * 60);
        setIndex(rb3, 60 * 60);
        setIndex(rb4, 60 * 60 * 24);
        setIndex(rb5, 60 * 60 * 24 * 7);


        fragment = KlineFragment.newInstance(market, 15 * 60);
        FragmentTransaction transaction_home = getSupportFragmentManager().beginTransaction();
        transaction_home.add(R.id.frm, fragment);
        transaction_home.commit();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_kline;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_fs:
                Intent intent = new Intent(mContext, TimeShareActivity.class);
                intent.putExtra("market", market);
                startActivity(intent);
                break;

            case R.id.tv_back:
                outPage();
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            outPage();
            return false;

        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    public void setIndex(RadioButton rb, final int tag) {
        rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    fragment = KlineFragment.newInstance(market, tag);
                    FragmentTransaction transaction_home = getSupportFragmentManager().beginTransaction();
                    transaction_home.replace(R.id.frm, fragment);
                    transaction_home.commit();
                }
            }
        });
    }

    public void outPage() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        fragment.stop();
        FragmentTransaction transaction_home = getSupportFragmentManager().beginTransaction();
        transaction_home.remove(fragment).commit();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }, 80);

    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e("TTTSS", newConfig.orientation + "");
        if (newConfig.orientation == 1) {

            EventBus.getDefault().post(new BaseEvent(5000));

            finish();
        }
    }
}
